
//
//  LevelModel.m
//  DaJiang
//
//  Created by sally on 16/10/14.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "LevelModel.h"

@implementation LevelModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"levelid" }];
}

//获取一级分类列表
+ (void)queryLevel1ListWithCompletion:(void (^)(NSMutableArray *levelList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          LevelModel *levelModel = [[LevelModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:levelModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  MainInterface *interface = [[MainInterface alloc] init];
  [interface queryLevel1ListWithCompletion:_completion error:_error];
}

//获取二级分类列表
+ (void)queryLevel2ListWithLevel1Id:(NSInteger)level1Id
                         completion:(void (^)(NSMutableArray *levelList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          LevelModel *levelModel = [[LevelModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:levelModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  MainInterface *interface = [[MainInterface alloc] init];
  [interface queryLevel2ListWithLevel1Id:level1Id completion:_completion error:_error];
}

@end

@implementation LevelData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end
